import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 邮箱
 * @date: 2018-7-25


 */
import { getResponse, createPagination, parseParameters } from 'utils/utils';
import { queryIdpValue, queryMapIdpValue, getPublicKey as _getPublicKey } from 'cpaas-front/lib/services/api';
import { fetchEmail as _fetchEmail, createEmail as _createEmail, updateEmail as _updateEmail, queryEmailServers as _queryEmailServers, fetchFilterList as _fetchFilterList, updateFilter as _updateFilter, deleteFilter as _deleteFilter, deleteEmail as _deleteEmail } from "../services/emailService";
export default {
  namespace: 'email',
  state: {
    emailList: [],
    // 邮箱数据
    emailProperties: [],
    // 服务器配置项列表
    tenantId: 0,
    // 租户id
    pagination: {},
    filterStrategyList: [],
    filterList: [],
    // 黑白名单列表
    filterPagination: {},
    // 黑白名单分页
    publicKey: '' // 密码公钥

  },
  effects: {
    // 获取值集
    fetchEnums: /*#__PURE__*/_regeneratorRuntime.mark(function fetchEnums(_, _ref) {
      var call, put, enumsRes, filterStrategyList, enums;
      return _regeneratorRuntime.wrap(function fetchEnums$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryMapIdpValue, {
                'CMSG.EMAIL_PROTOCOL': 'CMSG.EMAIL_PROTOCOL'
              });

            case 3:
              enumsRes = _context.sent;
              _context.t0 = getResponse;
              _context.next = 7;
              return call(queryIdpValue, 'CMSG.EMAIL.FILTER_STRATEGY');

            case 7:
              _context.t1 = _context.sent;
              filterStrategyList = (0, _context.t0)(_context.t1);
              enums = getResponse(enumsRes);

              if (!enums) {
                _context.next = 13;
                break;
              }

              _context.next = 13;
              return put({
                type: 'updateState',
                payload: {
                  enums: enums,
                  filterStrategyList: filterStrategyList
                }
              });

            case 13:
            case "end":
              return _context.stop();
          }
        }
      }, fetchEnums);
    }),
    // 获取邮箱账户
    fetchEmail: /*#__PURE__*/_regeneratorRuntime.mark(function fetchEmail(_ref2, _ref3) {
      var payload, put, call, res, data;
      return _regeneratorRuntime.wrap(function fetchEmail$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              put = _ref3.put, call = _ref3.call;
              _context2.next = 4;
              return call(_fetchEmail, parseParameters(payload));

            case 4:
              res = _context2.sent;
              data = getResponse(res);

              if (!data) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  emailList: data.content,
                  pagination: createPagination(data)
                }
              });

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchEmail);
    }),
    queryEmailServers: /*#__PURE__*/_regeneratorRuntime.mark(function queryEmailServers(_ref4, _ref5) {
      var payload, put, call, res, data;
      return _regeneratorRuntime.wrap(function queryEmailServers$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              put = _ref5.put, call = _ref5.call;
              _context3.next = 4;
              return call(_queryEmailServers, payload);

            case 4:
              res = _context3.sent;
              data = getResponse(res);

              if (!data) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  emailProperties: data.emailProperties
                }
              });

            case 9:
              return _context3.abrupt("return", data);

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, queryEmailServers);
    }),
    createEmail: /*#__PURE__*/_regeneratorRuntime.mark(function createEmail(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createEmail$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_createEmail, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, createEmail);
    }),
    updateEmail: /*#__PURE__*/_regeneratorRuntime.mark(function updateEmail(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateEmail$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_updateEmail, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, updateEmail);
    }),
    deleteEmail: /*#__PURE__*/_regeneratorRuntime.mark(function deleteEmail(_ref10, _ref11) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteEmail$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.next = 4;
              return call(_deleteEmail, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, deleteEmail);
    }),
    // 获取黑白名单
    fetchFilterList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchFilterList(_ref12, _ref13) {
      var payload, call, put, res, filter;
      return _regeneratorRuntime.wrap(function fetchFilterList$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call, put = _ref13.put;
              _context7.next = 4;
              return call(_fetchFilterList, parseParameters(payload));

            case 4:
              res = _context7.sent;
              filter = getResponse(res);
              _context7.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  filterList: filter && filter.content,
                  filterPagination: createPagination(filter)
                }
              });

            case 8:
              return _context7.abrupt("return", filter);

            case 9:
            case "end":
              return _context7.stop();
          }
        }
      }, fetchFilterList);
    }),
    // 更新黑白名单
    updateFilter: /*#__PURE__*/_regeneratorRuntime.mark(function updateFilter(_ref14, _ref15) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateFilter$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call;
              _context8.next = 4;
              return call(_updateFilter, payload);

            case 4:
              res = _context8.sent;
              return _context8.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context8.stop();
          }
        }
      }, updateFilter);
    }),
    // 删除黑白名单
    deleteFilter: /*#__PURE__*/_regeneratorRuntime.mark(function deleteFilter(_ref16, _ref17) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteFilter$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload;
              call = _ref17.call;
              _context9.next = 4;
              return call(_deleteFilter, payload);

            case 4:
              res = _context9.sent;
              return _context9.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, deleteFilter);
    }),
    // 请求公钥
    getPublicKey: /*#__PURE__*/_regeneratorRuntime.mark(function getPublicKey(_, _ref18) {
      var call, put, res;
      return _regeneratorRuntime.wrap(function getPublicKey$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              call = _ref18.call, put = _ref18.put;
              _context10.next = 3;
              return call(_getPublicKey);

            case 3:
              res = _context10.sent;

              if (!res) {
                _context10.next = 7;
                break;
              }

              _context10.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  publicKey: res.publicKey
                }
              });

            case 7:
              return _context10.abrupt("return", res);

            case 8:
            case "end":
              return _context10.stop();
          }
        }
      }, getPublicKey);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref19) {
      var payload = _ref19.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};